import json

from urllib import request, parse

from requests import Request


# url = 'http://httpbin.org/get'
#
# res = request.urlopen(url)
#
# print(res.status)
#
#
# if res.status == 200:
#     result = res.read()
#     result_text = res.decode('utf-8')
#     result_json = json.loads(result_text)
#     print(result_json['headers']['User-Agent'])
#     print(result_json['headers']['Content-Type'])


# from urllib import request, parse
# import json
#
# url = 'http://httpbin.org/post'
# data = {'name': 'zs'}
# url_encode = parse.urlencode(data)
#
# req = request.Request(
#     url,
#     data=url_encode.encode('utf-8'),
#     headers={
#         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36'
#     },
#     method='POST'
# )
# response = request.urlopen(req)
#
# if response.status == 200:
#     result = response.read().decode('utf-8')
#     result_json = json.loads(result)
#     print(result_json)
#     print(result_json['form'])

url = 'http://httpbin.org/post'
data = {'name': 'zs'}
json_encode = json.dumps(data)

req = request.Request(
    url,
    data=json_encode.encode('utf-8'),
    headers={
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
        'content-type':'application/json'
    },
    method='POST'
)
response = request.urlopen(req)

if response.status == 200:
    result = response.read().decode('utf-8')
    result_json = json.loads(result)
    print(result_json)
    print(result_json['form'])